
import java.util.*;
public class UserClass{
	
	UserNode head;
	int numberOfUsers;
	
	public String addUser(String url,String phoneNumber,String email){
		
		String newUser="newUser";
		String urlAdded="urlAdded";
		String maxOut="maxOut";
		UserNode traverse=null;
		
		if(numberOfUsers==0){
			head=new UserNode(url, phoneNumber,email,head);
			numberOfUsers++;
			return newUser;
		}
		else{
			Iterator iterator=UserClass.traverse(head);
				while(iterator.hasNext()){
					traverse=(UserNode)iterator.next();
				
						if(traverse.getPhoneNumber().equals(phoneNumber)){
					
							if(addUser(url,phoneNumber))return urlAdded;
							else return maxOut;
							
						}
				}
		}
		traverse.next=new UserNode(url, phoneNumber,email,traverse.next);
		numberOfUsers++;
		return newUser;
	}
	
	public boolean addUser(String url,String phoneNumber){
		/*
		 * each user is allowed two urls and one is set when the user first enters the system
		 */
		boolean value=true;//return =true is user is added false otherwise
		
		Iterator it = UserClass.traverse(head);
		while(it.hasNext()){
			UserNode node=(UserNode) it.next();
			if(node.getPhoneNumber().equals(phoneNumber)){
				if(node.getUrlArray(1)==null){
					node.setUrlArray(url,1);
					break;
				}
				else if(node.getUrlArray(1)!=null){
					value=false;
					break;
				}
			}
		}
		return value;
	}
    public String removeUser(String phoneNumber){
    	String number="This number is not listed in the system";
    	UserNode previousNode=null;
    	Iterator iterator=UserClass.traverse(head);
    	
    	if(head.getPhoneNumber().equals(phoneNumber)){
    	number=head.getPhoneNumber();
    	head=head.next;
    	numberOfUsers--;
    	}
    	else{
    		while(iterator.hasNext()){
    			
    			UserNode node=(UserNode) iterator.next();
    			if(node.getPhoneNumber().equals(phoneNumber)){
    			 number=node.getPhoneNumber();
    			 previousNode.next=previousNode.next.next;
    			 numberOfUsers--;
    			}
    			previousNode=node;
    		}
    	}
    	return number;
    }
	
	public static Iterator traverse(UserNode front){
		UserClass x = new UserClass();
		return x.new Traverse(front);
	}
	
	@SuppressWarnings("unchecked")
	private class Traverse implements Iterator{
		UserNode nextNode;
    public Traverse(UserNode head){
    	this.nextNode=head;
    }
	@Override
	public boolean hasNext() {
		return(nextNode!=null);
	}

	@Override
	public Object next() {
		
		if(nextNode!=null){
			UserNode nodeToReturn=nextNode;	
			nextNode=nextNode.next;
            return nodeToReturn;
		}
		else
            throw new NoSuchElementException("No next element");
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException("the method is not support here");
		
	}
 }
	
}